﻿import flash.display.BitmapData;
import mx.utils.Delegate;
import flash.geom.Rectangle;
import flash.geom.Point;
import mx.transitions.Tween;
import mx.transitions.easing.Regular;
import mx.transitions.easing.Strong;
/**
 * @author Jason
 */
class com.mentalaxis.flickr.lab.ImageCropTest {
	
	var canvas : MovieClip;
	var imageClip : MovieClip;
	var bitmapClip : MovieClip;
	var imageLoader : MovieClipLoader;
	var loaderListener : Object;
	var imageBitmap : BitmapData;
	var clipBitmap : BitmapData;
	var tweenDuration : Number = 1;
	var waitTime : Number = 3;
	var maxpos : Number = 55;
	var minpos : Number = 135;
	var max : Number = 190;
	var min : Number = 30;
	var inputText : TextField;
	var goButton : MovieClip;
	
	// 240 x 180 px image - from flickr, not that it matters at this stage.
	// We'll determine the image size programmatically and see if it's ok. 
	var imageURL : String =  "http://farm3.static.flickr.com/2191/2353634763_2fe414fc5e_b.jpg";
		
	public function dispose():Void {
		imageClip.removeMovieClip();
		bitmapClip.removeMovieClip();
		delete this;
	}	
	
	public function ImageCropTest(canvas:MovieClip, _imageURL:String) {
		this.canvas = canvas;
		imageClip = canvas.createEmptyMovieClip("imageClip", canvas.getNextHighestDepth());
		if(_imageURL.indexOf("http://") != -1 && _imageURL != undefined) 
		{
			trace ("valid http:");
			this.imageURL = _imageURL;
		}
		
		trace("Loading Image:" + this.imageURL);
		loaderListener = new Object();
		loaderListener.onLoadInit = Delegate.create(this, imageLoaded);		
		imageLoader = new MovieClipLoader();
		imageLoader.checkPolicyFile = true;
		imageLoader.addListener(loaderListener);
		imageLoader.loadClip(imageURL, imageClip);
	}
	
	private function imageLoaded():Void {
		trace("Image Loaded");
		trace("Width:" + imageClip._width);
		trace("Height:" + imageClip._height);
		
		// imageClip._visible = false;
		
		
		var clipRect : Rectangle;
		var offset : Number = offset = getOffset(imageClip._width, imageClip._height);
		var w:Number = imageClip._width;
		var h:Number = imageClip._height;
		var x:Number = 0;
		var y:Number = 0;
		
		imageBitmap = new BitmapData(w, h);
		imageBitmap.draw(imageClip);
		if(true) {
			if(w > h) {
				w = imageClip._width - (offset*2);
				h = imageClip._height;
				x = offset;
				y = 0;
			} else {
				w = imageClip._width;
				h = imageClip._height - (offset*2);
				y = offset;
				x = 0;
			}

			clipBitmap = new BitmapData(w, h);
			clipRect = new Rectangle(x, y, w, h);
			clipBitmap.copyPixels(imageBitmap, clipRect, new Point(0, 0));
			imageClip.removeMovieClip();
			imageClip = canvas.createEmptyMovieClip("imageClip", canvas.getNextHighestDepth());
			imageClip.attachBitmap(clipBitmap, imageClip.getNextHighestDepth(), "none", true);
		}
		imageClip._width = imageClip._height = 30;
		new Tween(imageClip, "_x", Strong.easeOut, minpos, maxpos, tweenDuration, true);
		new Tween(imageClip, "_y", Strong.easeOut, minpos, maxpos, tweenDuration, true);
		new Tween(imageClip, "_width", Strong.easeOut, min, max, tweenDuration, true);
		var a : Tween = new Tween(imageClip, "_height", Strong.easeOut, min, max, tweenDuration, true);
		a["onMotionFinished"] = Delegate.create(this, holdImage);
	}
	
	private function scaleUpFinished():Void {
		new Tween(imageClip, "_x", Strong.easeOut, maxpos, minpos, tweenDuration, true);
		new Tween(imageClip, "_y", Strong.easeOut, maxpos, minpos, tweenDuration, true);
		new Tween(imageClip, "_width", Strong.easeOut, max, min, tweenDuration, true);
		var a : Tween = new Tween(imageClip, "_height", Strong.easeOut, max, min, tweenDuration, true);
		a["onMotionFinished"] = Delegate.create(this, scaleDownFinished);
	}

	private function holdImage():Void {
		var o : Object = new Object();
		o.wait = 0;
		var a : Tween = new Tween(o, "wait", Strong.easeOut, 0, 100, waitTime, true);
		a["onMotionFinished"] = Delegate.create(this, scaleUpFinished);
	}

	private function scaleDownFinished():Void {
		new Tween(imageClip, "_x", Strong.easeOut, minpos, maxpos, tweenDuration, true);
		new Tween(imageClip, "_y", Strong.easeOut, minpos, maxpos, tweenDuration, true);
		new Tween(imageClip, "_width", Strong.easeOut, min, max, tweenDuration, true);
		var a : Tween = new Tween(imageClip, "_height", Strong.easeOut, min, max, tweenDuration, true);
		a["onMotionFinished"] = Delegate.create(this, holdImage);
	}
	
	private function getOffset(w:Number, h:Number):Number {
		return Math.abs(w - h)/2;
	}

}